import React, { useEffect } from 'react'
import style from './index.less'
import { Form, Input, Button, Checkbox } from 'antd';
import { connect } from 'dva'
import requestHooks from '../../serves/request.hooks'
export default connect((state) => ({ state,user: state.user }), {
    getLogin: (data) => ({
        type: 'user/getUset',
        data
    })
})(Form.create()((props) => {
    const handleSubmit = e => {
        e.preventDefault();
        props.form.validateFields((err, values) => {
            // console.log(values)
            if (!err) {
                // console.log( requestHooks('/api/login','post',values))
                props.getLogin(values)
                return
            }
        });
    };
    useEffect(() => {
        if (props.user.username) {
            props.history.push('/')
        }
    }, [props.user.username])
    const { getFieldDecorator } = props.form
    console.log(props.state)
    return (
        <div className={style.content}>
            <div className={style.form}>
                <Form onSubmit={handleSubmit} className="login-form">
                    <Form.Item>
                        {getFieldDecorator('username', {
                            rules: [{ required: true, message: 'Please input your username!' }],
                        })(
                            <Input
                                placeholder="Username"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item>
                        {getFieldDecorator('password', {
                            rules: [{ required: true, message: 'Please input your Password!' }],
                        })(
                            <Input
                                type="password"
                                placeholder="Password"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item>
                        {getFieldDecorator('remember', {
                            valuePropName: 'checked',
                            initialValue: true,
                        })(<Checkbox>Remember me</Checkbox>)}
                        <Button type="primary" htmlType="submit" className="login-form-button">
                            Log in
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        </div>
    )
}))